<?php
$mainSelector=array();
$findSelector=array();
class El
{

    public $backfun;
    public $argvSelector;
    public $arrKeyName;
    public $findstr;
    public $selectorStr;
    public $value;
    public $keys;
    public $mainSelector;
    public $findSelector;

    public function encodeKuohao($string,$name){
        $index=0;
        $kuohao=array();
        while(true){
            preg_match_all('/(\(([^\(\)]*)\))/', $string, $nn);
            if(count($nn[1])<1) break;
            for($i=0;$i<count($nn[1]);$i++){
                if(preg_match("/->[0-9a-zA-Z-]+".addsla($nn[1][$i])."/",$string)){
                    $string=str_replace($nn[1][$i],"《{\$GLOABLS[\"$name\"][{$index}]}》",$string);
                }else{
                    $string=str_replace($nn[1][$i],"{\$GLOABLS[\"$name\"][{$index}]}",$string);
                }

                $kuohao=array_merge($kuohao,array($index=>$nn[2][$i]));
                $index++;
            }
        }
        $GLOBALS[$name]=$kuohao;
        return $string;
    }

    private function decodeKuohao($selectorStr){
        $temp=$selectorStr;
        if(preg_match("/\{\\\$GLOABLS(\[.*\])\}/",$selectorStr,$nn)){
            $temp=str_replace($nn[0],eval("return \$GLOBALS{$nn[1]};"),$selectorStr);
        }
        $temp = str_replace("《", "(", $temp);
        $temp = str_replace("》", ")", $temp);
        return $temp;
    }

    private function decodeKuohao_all($selectorStr){
        $temp=$selectorStr;
        if(is_array($temp)){
            for ($i = 0; $i < count($temp); $i++) {
                $temp[$i]=$this->decodeKuohao_all($temp[$i]);
            }
        }else {
            preg_match_all("/\{\\\$GLOABLS(\[.*\])\}/Uisx", $temp, $nn);
            for ($i = 0; $i < count($nn[0]); $i++) {
                $temp = str_replace($nn[0][$i], eval("return \$GLOBALS{$nn[1][$i]};"), $temp);
            }
            $temp = str_replace("《", "(", $temp);
            $temp = str_replace("》", ")", $temp);
            if (preg_match("/\{\\\$GLOABLS(\[.*\])\}/", $temp)) {
                $temp = $this->decodeKuohao_all($temp);
            }
            $temp = preg_replace("/\.\^([a-z0-9A-Z]+)/", "[class^=\\1]", $temp);
            $temp = preg_replace("/\#\^([a-z0-9A-Z]+)/", "[id^=\\1]", $temp);
            $temp = preg_replace("/->[^\s]+/", "", $temp);
        }
        return $temp;
    }

    /**
     * @param $selectorStr
     * @return El
     */
    static public function Selector($selectorStr){
        $el2=new El();
        $selectorStr2=preg_replace_callback("/((?:[^\s\.\#>]|(?<!\-)>)*?)(\.|\\#)??(\^)??((?:[^>\-\(\)]|\-(?!>))*?)/Uisx",function($em) use(&$el2){
            if($em[1]==""&&$em[4]=="") return "";
            if($em[3]=="^") {
                if ($em[2] == "#") {
                    $selectorStr="{$em[1]}[id^=\"{$em[4]}\"]{$em[5]}";
                } elseif ($em[2] == ".") {
                    $selectorStr="{$em[1]}[class^=\"{$em[4]}\"]{$em[5]}";
                }
            }else{
                $selectorStr="{$em[1]}{$em[2]}{$em[4]}{$em[5]}";
            }
            return $selectorStr;
        },$selectorStr);
        $el2->findEls=array();
        if(preg_match("/>>(.*)/",$selectorStr2,$nn)) {
            $nn33=explode(",",trim($nn[1]));
            $findEls=array();
            for($i=0;$i<count($nn33);$i++){
                if($nn33[$i]=="") continue;
                if(preg_match("/\(\?([a-z]+)~([^\)]*)\)/",$nn33[$i],$nn11)) {
                    $findEls['name'] = $nn11[1];
                }else{
                    $findEls['name']="";
                }
                if(preg_match("/->((?:[a-z0-9A-Z\(\)\"\'\>\+\*\.\$\^\/\=\&\?\#\%\-\\\]|\-(?!>))*)/",$nn33[$i],$nn11)) {
                    $findEls['backfun'] = $nn11[1];
                }else{
                    $findEls['backfun'] = "";
                }
                if( $findEls['backfun']==""){
                    $findEls['backfun']="text()";
                }
                $findEls['findstr']=preg_replace("/\(\?([a-z]+)~([^\)]*)\)|->(.*)/","",$nn33[$i]);
                if($findEls['findstr']!=""){
                    $el2->findEls[]=$findEls;
                }

            }
            $selectorStr2=preg_replace("/>>(.*)/","",$selectorStr2);
        }

        if(preg_match("/\(\?([a-z]+)~([^\)]*)\)/",$selectorStr2,$nn)) {
            $el2->arrKeyName = $nn[1];
            $el2->argvSelector = $nn[2];
        }
        if(preg_match("/->([a-z0-9A-Z\(\)\"\'\-\>\+\*\.\$\^\/\=\&\?\#\%\-\\\]*)/",$selectorStr2,$nn)) {
            $el2->backfun = $nn[1];
        }
        if($el2->backfun==""){
            $el2->backfun="text()";
        }

        $selectorStr2=preg_replace("/->[^\s]+/","",$selectorStr2);
        $selectorStr2 = preg_replace("/\(\?[a-z]+~(.*)\)/", "", $selectorStr2);
        $selectorStr2 = preg_replace("/>>(.*)/", "", $selectorStr2);
        $el2->selectorStr=$selectorStr2;
        return $el2;
    }

    function pq($selectorStr,$kongIndex=0){
        $this->selectorStr=$selectorStr;
        $nn=explode(" ",$selectorStr);
        $this->mainSelector=self::Selector($nn[0]);
        $this->findSelector=self::Selector($nn[1]);
        return $this;
    }

    Static function cldata($data){
        if(is_array($data)&&count($data)===1&&is_array($data[0])&&count($data[0])===1&&is_array($data[0][0])&&count($data[0][0])===1){
            $result= $data[0][0][0];
        }else if(is_array($data)&&count($data)==1&&is_array($data[0])&&count($data[0])==1){
            $result= $data[0][0];
        }else if(is_array($data)&&count($data)==1){
            $result= $data[0];
        }else{
            $result= $data;
        }
        $result=preg_replace("/[\n\r\s]+/",",",trim($result));
        return $result;
    }

    public function trims($str){
        $str=preg_replace("/\n|：/","",$str);
        $str=trim($str);
        return $str;
    }
}